/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.mobile.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.w3c.dom.Document;
import com.cmcc.zysoft.infocollection.common.BaseController;
import com.cmcc.zysoft.infocollection.mobile.service.MInterfaceLogService;
import com.cmcc.zysoft.infocollection.mobile.service.MResourceService;
import com.cmcc.zysoft.infocollection.mobile.service.MSystemUserService;
import com.cmcc.zysoft.infocollection.model.SystemUser;

/**
 * @author 袁凤建
 * <br />邮箱: yuan.fengjian@ustcinfo.com
 * <br />描述: MResourceController.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-8-12 下午4:43:36
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Controller
@RequestMapping("/mobile/resource")
public class MResourceController extends BaseController {

	/**
	 * 日志.
	 */
	private static final Logger logger = LoggerFactory.getLogger(MResourceController.class);
	
	/**
	 * 属性名称: mResourceService.
	 * 类       型: MResourceService.
	 */
	@javax.annotation.Resource
	private MResourceService mResourceService;
	
	/**
	 * 属性名称: mSystemUserService.
	 * 类       型: MSystemUserService.
	 */
	@Resource
	private MSystemUserService mSystemUserService;
	
	/**
	 * 属性名称: mInterfaceLogService.
	 * 类       型: MInterfaceLogService.
	 */
	@Resource
	private MInterfaceLogService mInterfaceLogService;
	
	@RequestMapping("/main.htm")
	public String main() {
		return "/pc/resource/test";
	}
	
	/**
	 * 信息新增接口-通过该接口向中央信息库中新增商户信息,并将商户信息持久化至本地数据库.
	 * @param usercode 用户手机号
	 * @param caterReg 区/县ID
	 * @param caterServ 服务项目ID
	 * @param caterName 商户名称
	 * @param caterStore 分店名
	 * @param shortName 别名简称
	 * @param caterMobile 联系电话
	 * @param caterDetailAdd 详细地址
	 * @param caterBusi 商圈
	 * @param caterBuild 标志性建筑
	 * @param caterZip 邮编
	 * @param caterTime 营业时间
	 * @param caterTransit 公交路线
	 * @param caterDesc 位置描述
	 * @param caterAtmo 商户氛围
	 * @param caterReco 推荐点
	 * @param caterMerIntr 商户简介
	 * @param caterbiaoqian 标签
	 * @param caterDish 招牌菜/特色菜
	 * @param caterPark 停车位
	 * @param caterLevel 消费档次
	 * @param caterSwingCard 是否刷卡(0-是,1-否)
	 * @param caterArea 面积(m2)
	 * @param caterRoom 包间信息
	 * @param caterBigRoom 大包间
	 * @param caterSmallRoom 小包间
	 * @param caterSeatCap 客容量
	 * @param caterAccReser 是否接受预定(0-是,1-否)
	 * @param caterDeliServ 是否外送服务(0-是,1-否)
	 * @param caterInfoMainte 信息维护
	 * @param x 经度
	 * @param y 纬度
	 * @param mobilePayment 是否手机支付(0-是,1-否)
	 * @return Map<String, Object>
	 */
	@RequestMapping("/insert.htm")
	@ResponseBody
	public Map<String, Object> addResource(String usercode, String caterReg, String caterServ, String caterName, String caterStore, 
			String shortName, String caterMobile, String caterDetailAdd, String caterBusi, String caterBuild, String caterZip, 
			String caterTime, String caterTransit, String caterDesc, String caterAtmo, String caterReco, String caterMerIntr, 
			String caterbiaoqian, String caterDish, String caterPark, String caterLevel, String caterSwingCard, String caterArea, 
			String caterRoom, String caterBigRoom, String caterSmallRoom, String caterSeatCap, String caterAccReser, 
			String caterDeliServ, String caterInfoMainte, String x, String y, String mobilePayment) {
		System.out.println("================================================================================");
		System.out.println("新增商户：");
		System.out.println("手机号码：" + usercode);
		System.out.println("================================================================================");
		
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<SystemUser> list = this.mSystemUserService.findByNamedParam(new String[]{"userName", "delFlag"}, new Object[]{usercode, "0"});
			if(null != list && !list.isEmpty()) {
				SystemUser user = list.get(0);
				System.out.println("用户ID ：" + user.getUserId());
				System.out.println("================================================================================");
				this.mResourceService.addResource(user.getUserId(), caterReg, caterServ, caterName, caterStore,
						shortName, caterMobile, caterDetailAdd, caterBusi, caterBuild, caterZip, caterTime, caterTransit, 
						caterDesc, caterAtmo, caterReco, caterMerIntr, caterbiaoqian, caterDish, caterPark, caterLevel, 
						caterSwingCard, caterArea, caterRoom, caterBigRoom, caterSmallRoom, caterSeatCap, caterAccReser, 
						caterDeliServ, caterInfoMainte, x, y, mobilePayment);
				this.mInterfaceLogService.addLog(user.getUserId(), user.getCompanyId(), "新增商户信息");
				map.put("status", "1");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("status", "2");
			logger.debug("#新增商户信息失败!\n{}", e.getMessage());
		}
		return map;
	}
	
	/**
	 * 信息更新接口-通过该接口,更新中央信息库中的商户信息,并将商户信息持久化至本地数据库.
	 * @param usercode 用户手机号
	 * @param shopId 商户ID
	 * @param caterReg 区/县ID
	 * @param caterServ 服务项目ID
	 * @param caterName 商户名称
	 * @param caterStore 分店名
	 * @param shortName 别名简称
	 * @param caterMobile 联系电话
	 * @param caterDetailAdd 详细地址
	 * @param caterBusi 商圈
	 * @param caterBuild 标志性建筑
	 * @param caterZip 邮编
	 * @param caterTime 营业时间
	 * @param caterTransit 公交路线
	 * @param caterDesc 位置描述
	 * @param caterAtmo 商户氛围
	 * @param caterReco 推荐点
	 * @param caterMerIntr 商户简介
	 * @param caterbiaoqian 标签
	 * @param caterDish 招牌菜
	 * @param caterPark 停车位
	 * @param caterLevel 消费档次
	 * @param caterSwingCard 是否刷卡(0-是,1-否)
	 * @param caterArea 面积(m2)
	 * @param caterRoom 包间信息
	 * @param caterBigRoom 大包间
	 * @param caterSmallRoom 小包间
	 * @param caterSeatCap 客容量
	 * @param caterAccReser 是否接受预定(0-是,1-否)
	 * @param caterDeliServ 是否外送服务(0-是,1-否)
	 * @param caterInfoMainte 信息维护
	 * @param x 经度
	 * @param y 纬度
	 * @param mobilePayment 是否手机支付(0-是,1-否)
	 * @return Map<String, Object>
	 */
	@RequestMapping("/update.htm")
	@ResponseBody
	public Map<String, Object> updateResource(String usercode, String shopId, String caterReg, String caterServ, String caterName,
			String caterStore, String shortName, String caterMobile, String caterDetailAdd, String caterBusi, String caterBuild,
			String caterZip, String caterTime, String caterTransit, String caterDesc, String caterAtmo, String caterReco, 
			String caterMerIntr, String caterbiaoqian, String caterDish, String caterPark, String caterLevel, String caterSwingCard,
			String caterArea, String caterRoom, String caterBigRoom, String caterSmallRoom, String caterSeatCap, String caterAccReser, 
			String caterDeliServ, String caterInfoMainte, String x, String y, String mobilePayment) {
		System.out.println("================================================================================");
		System.out.println("更新商户：");
		System.out.println("手机号码：" + usercode);
		System.out.println("================================================================================");
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<SystemUser> list = this.mSystemUserService.findByNamedParam(new String[]{"userName", "delFlag"}, new Object[]{usercode, "0"});
			if(null != list && !list.isEmpty()) {
				SystemUser user = list.get(0);
				System.out.println("用户ID ：" + user.getUserId());
				System.out.println("================================================================================");
				this.mResourceService.updateResource(user.getUserId(), shopId, caterReg, caterServ, caterName, 
						caterStore, shortName, caterMobile, caterDetailAdd, caterBusi, caterBuild, caterZip, caterTime, 
						caterTransit, caterDesc, caterAtmo, caterReco, caterMerIntr, caterbiaoqian, caterDish, caterPark, 
						caterLevel, caterSwingCard, caterArea, caterRoom, caterBigRoom, caterSmallRoom, caterSeatCap, 
						caterAccReser, caterDeliServ, caterInfoMainte, x, y, mobilePayment);
				this.mInterfaceLogService.addLog(user.getUserId(), user.getCompanyId(), "更新商户信息");
				map.put("status", "1");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("status", "2");
			logger.debug("#更新商户信息失败!\n{}", e.getMessage());
		}
		return map;
	}
	
	/**
	 * 生活(商户)搜索接口-展示商户列表.
	 * @param pid 合作类型
	 * @param vt 接入类型(1-WAP,2-客户端,5-WEB)
	 * @param pt 产品类型(2-商户)
	 * @param ip 用户IP
	 * @param uci 接入地
	 * @param m 手机号
	 * @param ua 手机UA信息
	 * @param ui 用户信息
	 * @param ls 定位串
	 * @param q 查询串
	 * @param pn 查询页数
	 * @param ps 每页条数
	 * @param lpi 省ID
	 * @param lci 市ID
	 * @param lti 行业ID
	 * @param x 经度
	 * @param y 纬度
	 * @param lbb 范围
	 * @param discount 优惠券查询
	 * @param DiscountRate 折扣率
	 * @param address 商圈查询
	 * @param customize 复杂查询参数
	 * @param Zip 是否压缩
	 * @return document
	 */
	@RequestMapping("/search.htm")
	@ResponseBody
	public Document search(String pid, String vt, String pt, String ip, String uci, String m, String ua, String ui, String ls, 
			String q, String pn, String ps, String lpi, String lci, String lti, String x, String y, String lbb, String discount,
			String DiscountRate, String address, String customize, String Zip) {
		Date date1 = new Date();
		Document document = null;
		try {
			List<SystemUser> list = this.mSystemUserService.findByNamedParam(new String[]{"userName", "delFlag"}, new Object[]{m, "0"});
			if(null != list && !list.isEmpty()) {
				SystemUser user = list.get(0);
				document = this.mResourceService.search(pid, vt, pt, ip, uci, m, ua, ui, ls, q, pn, ps, lpi, lci, lti, x, y, lbb, 
						discount, DiscountRate, address, customize, Zip);
				this.mInterfaceLogService.addLog(user.getUserId(), user.getCompanyId(), "商户搜索");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug("#商户搜索失败!\n{}", e.getMessage());
		}
		Date date2 = new Date();
		System.out.println("================================================================================");
		System.out.println("接口响应时间 : " + (date2.getTime() - date1.getTime()));
		System.out.println("================================================================================");
		return document;
	}
	
	/**
	 * 商户详情接口-查询商户的详细信息.
	 * @param pid 合作类型(合作方ID)
	 * @param vt 接入类型(1-WAP,2-客户端,5-WEB)
	 * @param pt 产品类型(2-商户)
	 * @param ip 用户访问IP(PC Web)
	 * @param uci 用户手机接入地
	 * @param m 手机号
	 * @param ua 手机UA信息(用户手机的型号)
	 * @param ui 用户信息(Cookie记录的用户信息)
	 * @param ls 定位串
	 * @param ii 索引信息ID(info_id:流水号)
	 * @param Zip 是否压缩
	 * @return String
	 */
	@RequestMapping("/detail.htm")
	@ResponseBody
	public String detail(String pid, String vt, String pt, String ip, String uci, String m, String ua, String ui, String ls, String ii,
			String Zip) {
		String result = "";
		try {
			List<SystemUser> list = this.mSystemUserService.findByNamedParam(new String[]{"userName", "delFlag"}, new Object[]{m, "0"});
			if(null != list && !list.isEmpty()) {
				SystemUser user = list.get(0);
				result = this.mResourceService.detail(pid, vt, pt, ip, uci, m, ua, ui, ls, ii, Zip);
				this.mInterfaceLogService.addLog(user.getUserId(), user.getCompanyId(), "查看商户详情");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug("#查看商户详情失败!\n{}", e.getMessage());
		}
		return result;
	}
}